package router

import (
	"log"
	global "yundata-service/internal"
	"yundata-service/util"

	"github.com/gin-gonic/gin"
)

type TokenAction struct {
	global.ActionModel
}

func CheckToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		ac := TokenAction{}
		token := c.GetHeader("Authorization")

		ac.Ctx = c
		// 检验token
		items, err := util.ParseToken(token, "hongsi")
		if err != nil {
			log.Println("error......")
			ac.Fail(err.Error())
			// abort 不会结束当前
			c.Abort()
			return
		} else {
			c.Set("user", items)
			c.Next()
		}
	}
}

func ActionMiddleware(action *global.ActionModel) gin.HandlerFunc {
	return func(c *gin.Context) {
		action.Ctx = c
		c.Next()
	}
}
